Getting Sum of Array Elements in JavaScript

To get the sum of array elements in JavaScript, you can use the array.reduce(callback, initialValue) method. The array.reduce() method invokes the callback function once for each array element and passes the result of the callback function call to the next call. The sum of the array elements will be returned as the result of the array.reduce() method. Alternatively, you can find the sum of array elements using a "for" loop. In this JavaScript Array Sum Example, we use the reduce() method to get the sum of the array elements with an initial value. Below you can see more examples of calculating the sum of JavaScript array elements with detailed descriptions. Click Execute to run the JavaScript Array Sum Example online and see the result.
Getting Sum of Array Elements in JavaScript Execute
let array =  [1, 2, 3, 4, 5];

let result = array.reduce((a, b) => {
  return a + b;
}, 10);

console.log(result);
Updated: Viewed: 17634 times

What is an array in JavaScript?

An array in JavaScript is a collection of data that is designed to store multiple values in a single variable. A JavaScript array is created using square brackets "[...]" and enumerating the array elements using commas or by using the "new Array()" constructor. Each value in the array has its serial number (index), which allows you to access the element by index. In JavaScript, arrays start at index zero. JavaScript provides a rich set of built-in functions for joining, slicing, reversing, merging, copying, containing, cloning, inserting, clearing, and reducing arrays. You can also convert string to array, convert array to JSON, convert array to string, and get a sum of array elements.

How to find the sum of array elements in JavaScript?

To find the sum of array elements in JavaScript, you can use the array.reduce(function(total, currentValue, currentIndex, array), initialValue) method. The callback function will not be called on removed or missing array elements. The array.reduce() method does not change the array it was called on. The range of elements processed by the reduce() method is set before the first call to the callback function. If new elements have been added to the array since the reduce() method was called, then the callback function will not be called for those elements.

The return value of the callback function is provided as an argument for the following callback function call, causing the last call's return value to become the method's return value.

JavaScript Array reduce() Example
let array = [1, 2, 3, 4, 5];

let sum = array.reduce(function(a, b){
  return a + b;
});

console.log(sum);

// output: 15

JavaScript Sum Array Examples

The following are examples of getting the sum of array elements in JavaScript:

Getting the sum of array elements

The following is an example of getting the sum of the elements of a JavaScript array using the array.reduce() method and an initial value:

JavaScript Array Sum Example
let array =  [10, 12, 28];

let result = array.reduce((x, y) => {
  return x + y;
}, 10);

console.log(result);

// output: 60

Getting the sum of array elements using a forEach loop

The following is an example of getting the sum of the elements of a JavaScript array using a forEach loop:

JavaScript Array Sum with forEach Example
let array = [3, 5, 4, 6];
let sum = 0;

array.forEach(item => {
    sum += item;
});

console.log(sum);

// output: 18

Getting the sum of array elements using the map() method

The array.map() method is new in ES6. The following is an example of getting the sum of the elements of a JavaScript array using the map() method:

JavaScript Array Sum with map() Example
let array = [14, 23, 11, 8, 31];
let sum = 0;

array.map(x => sum += x);

console.log(sum);

// output: 87

Getting the sum of array elements using a for loop

In JavaScript, the for loop is used to iterate over an iterable object. We can use a for loop to loop through the array elements and sum their values. The following is an example of getting the sum of the elements of a JavaScript array using a for loop:

JavaScript Array Sum with for loop Example
let array = [10, 20, 30, 40, 50];
let sum = 0;

for (let i = 0; i < array.length; i++) {
  sum += array[i];
}

console.log(sum);

// output: 150

Getting the sum of array elements using a for/of loop

The following is an example of getting the sum of the elements of a JavaScript array using a for/of loop:

JavaScript Array Sum with for/of loop Example
let array = [3, 2, 7, 9, 5];
let sum = 0;

for (let x of array){
    sum += x;
}
console.log(sum);

// output: 26

Getting the sum of array elements using a while loop

The following is an example of getting the sum of the elements of a JavaScript array using a while loop:

JavaScript Array Sum with while loop Example
let array = [3, 5, 17, 22, 19];
let sum = 0;
let i = -1;

while (++i < array.length) {
  sum += array[i];
}

console.log(sum);

// output: 66

See also